package org.lagou.Dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import org.lagou.Dao.GetResumeDao;
import org.lagou.bean.GetResumeBean;
import org.lagou.resultsethandler.GetResumeBeanResultSetHandler;

public class GetResumeDaoImpl extends BaseDao implements GetResumeDao {

	@Override
	public List<GetResumeBean> getGetResumeAll() {
		try {
			return query.query("select * from getresume", new BeanListHandler<GetResumeBean>(GetResumeBean.class));
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public List<GetResumeBean> getGetResumePagin(int pageIndex, int pageSize) {
		try {
			return query.query("select * from getresume limit ?,?",
					new BeanListHandler<GetResumeBean>(GetResumeBean.class), (pageIndex - 1) * pageSize, pageSize);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public long getGetResumeCount() {
		try {
			return query.query("select count(1) from getresume", new ScalarHandler<Long>());
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return -1;
	}

	@Override
	public int addGetResume(GetResumeBean info) {
		try {
			return query.update(
					"insert into getresume(companyid,positionid,resumeid,state,companyname,positionname,userid,date) values(?,?,?,?,?,?,?,?)",
					info.getCompanyid(), info.getPositionid(), info.getResumeid(), info.getState(),
					info.getCompanyname(), info.getPositionname(), info.getUserid(), info.getDate());
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return -1;
	}

	@Override
	public int updateGetResume(GetResumeBean info) {
		try {
			return query.update(
					"update getresume set companyid=?,positionid=?,resumeid=?,state=?,companyname=?,positionname=?,userid=?,date=? where id=?",
					info.getCompanyid(), info.getPositionid(), info.getResumeid(), info.getState(),
					info.getCompanyname(), info.getPositionname(), info.getUserid(), info.getDate(), info.getId());
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return -1;
	}

	@Override
	public int deleteGetResume(int id) {
		try {
			return query.update("delete from getresume where id=?", id);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return -1;
	}

	@Override
	public List<GetResumeBean> getGetResumeByCompanyid(int companyid) {
		try {
			return query.query(
					"select g.*,r.resumename,r.name,r.sex,r.education,r.edexperience,r.img,r.city,r.oldposition,r.oldcompany,r.school,r.tel,r.email from resume r,getresume g where g.resumeid=r.id and g.companyid=?",
					new GetResumeBeanResultSetHandler(), companyid);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public List<GetResumeBean> getGetResumeByResumeid(int resumeid) {
		try {
			return query.query("select * from getresume where resumeid=?", new GetResumeBeanResultSetHandler(),
					resumeid);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}

	public int updatestate(int id) {
		try {
			return query.update("update getresume set state=3 where id=?", id);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return 0;
	}

	@Override
	public int updatestatee(int id) {
		try {
			return query.update("update getresume set state=2 where id=?", id);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return 0;
	}

	@Override
	public List<GetResumeBean> getGetResumeByState(int userid,int state) {
		try {
			return query.query("select * from getresume where userid=? and state=?", new BeanListHandler<GetResumeBean>(GetResumeBean.class),
					userid,state);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}

}
